/**
 * Created by Administrator on 2017/4/5.
 */

var Vue = require('vue');
var axios=require('com/axios.min')
var qs=require('qs');

Vue.prototype.$bus=Vue.prototype.$bus||new Vue(); //类似于bus，全局调用，不推荐
/*开启debug模式*/
Vue.config.debug=true;

//header中加入token验证
var rights_manage_userinfo={token:'1',adminid:'1'}
if(localStorage.getItem('location_manage_web_token')){
    rights_manage_userinfo=JSON.parse(localStorage.getItem('location_manage_web_token'))
}
var instance=axios.create({
    time:15000, //超时时间
/*    baseURL:window.location.origin,*/
    headers:{'Token':rights_manage_userinfo.token,'Adminid':rights_manage_userinfo.adminid},
})

//兼容IE
if (window["context"] == undefined) {
    if (!window.location.origin) {
        window.location.origin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port: '');
    }
    window["context"] = location.origin+"/V6.0";
}


var apiUrl=window.location.origin+'/api/v2/';
if(process.env.NODE_ENV=='production')
  apiUrl='http://permission.joyincar.cn/api/v2/';
else  if(process.env.NODE_ENV=='beta')
  apiUrl='http://t.warning.lifesonic.cn/backend/';
else    apiUrl='http://t.warning.lifesonic.cn/backend/';

const fetch=(url,params,type)=>{
    let headers={}
    if(typeof type==='undefined')
        return instance.request({
            url:apiUrl+url,
            params:params,
            type:'get',
            withCredentials: true, //跨域设置，request和get区别设置不同
        }).then(response=>response.data).then(res=>checkToken(res))
        // return instance.get(apiUrl+url,{params:params},{withCredentials: true}).then(response=>response.data).then(res=>checkToken(res))
    else if(type==1) return instance.post(apiUrl+url,qs.stringify(params),{withCredentials: true}).then(response=>response.data).then(res=>checkToken(res))
    else if(type==-1) return instance.get(url,{params:params}).then(response=>response.data).then(res=>checkToken(res))
}

const checkToken=res=>{
    if(res&&res.result){
        if(res.result===-999999){
            //清除Storage  token信息
            localStorage.removeItem('location_manage_web_token')
             window.location.href='login.html'
        }else{  //统一错误处理
            if(typeof store!=='undefined'&&store.dialog)
                store.dialog({
                message:res.msg,
                type:'error',
                duration:10000,
            })

        }
    }
    //并判断loading是否在加载中
    if(typeof store!=='undefined'&&store.loading){
        store.loading.hide()
    }
    return res
}

Vue.prototype.$http={
    get:null,
    post:null,
    otherget:null, //第三方域名请求使用
}

Vue.prototype.$http.get=(url,params)=>{
    return  fetch(url,params)
}
Vue.prototype.$http.post=(url,params)=>{
    return  fetch(url,params,1)
}

Vue.prototype.$http.otherget=(url,params)=>{  //第三方域名请求使用
    return  fetch(url,params,-1)
}

//自定义过滤器

//时间戳转dateformat
Vue.filter('toLocaleDate', function (value) {

    var ms = (+value) * 1000;
    var date = new Date(ms);

    var year = date.getFullYear();
    var mouth = date.getMonth() + 1;
    if (mouth  < 10) {
        mouth = '0' + mouth;
    };
    var day = date.getDate();
    if (day  < 10) {
        day = '0' + day;
    };
    var hours = date.getHours();
    if (hours < 10) {
        hours = '0' + hours;
    };
    var minutes = date.getMinutes();
    if (minutes < 10) {
        minutes = '0' + minutes;
    };
    var sec = date.getSeconds();
    if (sec  < 10) {
        sec  = '0' + sec ;
    };
    return year + '-' + mouth + '-' + day + '  ' +  hours + ':' +  minutes+':' +sec;
});



module.exports=Vue